package com.chencs.biz.shop.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.biz.shop.mapper.ShopMapper;
import com.chencs.biz.shop.service.ShopService;
import com.chencs.vo.biz.product.ProductModel;
import com.chencs.vo.biz.shop.NewShopModel;
import com.chencs.vo.biz.shop.ShopModel;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by fangzehua on 2017/8/15.
 */

@RestController
@RequestMapping("/shop")
public class ShopController {


    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "/shopList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String shopList() {
        List<ShopModel> list = shopService.getShopList();
        if (!list.isEmpty()) {
            return JSON.toJSONString(list).toString();
        } else {
            return ResponseOptUtil.commonMsgError("获取店鋪列表失败");
        }
    }


    @RequestMapping(value = "/findShopById", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findShopById(@RequestBody String id) {
        ShopModel model = shopService.findShopById(id);
        if (model !=null) {
            return JSON.toJSONString(model).toString();
        } else {
            return ResponseOptUtil.commonMsgError("获取店鋪失败");
        }
    }

    @RequestMapping(value = "/insertShop", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String insertShop(@RequestBody ShopModel model) {
        try {
            shopService.insertShop(model);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("添加失败");
        }

    }

    @RequestMapping(value = "/updateShop", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String updateShop(@RequestBody ShopModel model) {
        try {
            shopService.updateShop(model);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("修改失败");
        }
    }

    @RequestMapping(value = "/delShopById", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String delShopById(@RequestBody String sid) {
        try {
            shopService.delShopById(sid);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("删除失败");
        }
    }



    @RequestMapping(value = "/newshopList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String newshopList() {
        List<NewShopModel> list = shopService.getNewShopList();
        if (!list.isEmpty()) {
            return JSON.toJSONString(list).toString();
        } else {
            return ResponseOptUtil.commonMsgError("获取店鋪列表失败");
        }
    }




    @RequestMapping(value = "/updateShopModel", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String updateShopModel(@RequestBody NewShopModel model) {
        try {
            shopService.updateShopModel(model);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("修改失败");
        }
    }




    @RequestMapping(value = "/delShopModel", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String delShopModel(@RequestBody String sid) {
        try {
            shopService.delShopModel(sid);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("删除失败");
        }
    }


    @RequestMapping(value = "/addShopModel", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String addShopModel(@RequestBody NewShopModel model) {
        try {
            shopService.addShopModel(model);
            return  ResponseOptUtil.commonSucess();
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("添加店铺失败");
        }
    }



    @RequestMapping(value = "/findNewShopModel", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findNewShopModel(@RequestBody String id) {
        NewShopModel model = shopService.findNewShopModel(id);
        if (model !=null) {
            return JSON.toJSONString(model).toString();
        } else {
            return ResponseOptUtil.commonMsgError("获取店鋪失败");
        }
    }

}
